package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.domain.CustomerTransferHistory;
import cn.congzhou.crm.query.CustomerTraceHistoryQueryObject;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.ICustomerTransferHistoryService;
import cn.congzhou.crm.service.ISystemDictionaryItemService;
import cn.congzhou.crm.utils.ResultJSON;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
@RequestMapping("/customerTransferHistory")
public class CustomerTransferHistoryController {

    private Logger logger = LoggerFactory.getLogger(CustomerTransferHistoryController.class);

    @Autowired
    private ICustomerTransferHistoryService customerTransferHistoryService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiresPermissions(value = {"查询移交历史","customerTransferHistory:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerTraceHistoryQueryObject qo, Model model) {
        PageInfo<CustomerTransferHistory> pageInfo = customerTransferHistoryService.selectByPage(qo);
        model.addAttribute("ro",pageInfo);

        return "/customerTransferHistory/list";
    }



    @RequestMapping("/save")
    @RequiresPermissions(value = {"保存移交历史","customerTransferHistory:save"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(CustomerTransferHistory entity) {
        ResultJSON result = new ResultJSON();
        try {
            customerTransferHistoryService.saveOrUpdate(entity);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }

    //==========================添加ligerUI添加的方法============================================//


    @RequestMapping(value = "/toCustomerTransferHistoryGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查询移交历史","customerTransferHistory:query"},logical = Logical.OR)
    public String toCustomerTransferHistoryGrid(){

        return "/customerTransferHistory/customerTransferHistoryGrid";
    }


    @RequestMapping(value = "/getCustomerTransferHistoryData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查询移交历史","customerTransferHistory:query"},logical = Logical.OR)
    public Object getCustomerTransferHistoryData(CustomerTraceHistoryQueryObject qo, HttpServletResponse response){
        PrintWriter writer = null;
        try{
            PageInfo<CustomerTransferHistory> pageInfo = customerTransferHistoryService.selectByPage(qo);
            String json = JSON.toJSONString(pageInfo);
            writer = response.getWriter();
            writer.println(json);
            writer.flush();
        }catch (Exception e){
            logger.error("响应数据失败",e);
        }finally {
            if(writer != null){
                writer.close();
            }
        }

        return null;
    }



}
